package com.txm.qqview;

import com.txm.qqcommon.Message;
import com.txm.qqcommon.MessageType;
import com.txm.service.ClientConnectServerThread;
import com.txm.service.UserClientService;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

public class Login {

    public static void main(String[] args) {
        // 创建一个JFrame
        JFrame frame = new JFrame("QQ登录");
        frame.setSize(400, 300); // 设置窗口大小
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false); // 不可改变视口大小

        // 创建一个主面板，并设置布局为null
        JPanel panel = new JPanel();
        panel.setLayout(null);

        // 创建和配置JLabel和JTextField
        JLabel usernameLabel = new JLabel("账号:");
        usernameLabel.setBounds(50, 50, 50, 30);
        panel.add(usernameLabel);

        JTextField usernameField = new JTextField();
        usernameField.setBounds(120, 50, 200, 30);
        panel.add(usernameField);

        // 创建和配置密码JLabel和JPasswordField
        JLabel passwordLabel = new JLabel("密码:");
        passwordLabel.setBounds(50, 100, 50, 30);
        panel.add(passwordLabel);

        JPasswordField passwordField = new JPasswordField();
        passwordField.setBounds(120, 100, 200, 30);
        panel.add(passwordField);

        // 创建和配置登录按钮
        JButton loginButton = new JButton("登录");
        loginButton.setBounds(150, 160, 100, 30);
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = usernameField.getText();
                String password = new String(passwordField.getPassword());
                // 显示等待窗口
                SwingUtilities.invokeLater(() -> {
                    JOptionPane.showMessageDialog(frame, "登录中...");
                });
                new Thread(() -> {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException ex) {
                        throw new RuntimeException(ex);
                    }
                    UserClientService userClientService = new UserClientService();
                    Message message = userClientService.checkUser(username, password);
                    System.out.println("服务器返回的数据：" + message);
                    if (message.getMsgType().equals(MessageType.MESSAGE_LOGIN_SUCCEED)) {
                        JOptionPane.showMessageDialog(frame, message.getContent());
                        // 关闭登录页面
                        frame.dispose();
                        QQView qqView = new QQView();
                        qqView.userName = message.getGetter();
//                        qqView.getMainView();
                        ClientConnectServerThread clientConnectServerThread = new ClientConnectServerThread(userClientService.getSocket(), qqView);
                        clientConnectServerThread.start();
                    } else {
                        JOptionPane.showMessageDialog(frame, message.getContent());
                        try {
                            userClientService.getSocket().close();
                        } catch (IOException ex) {
                            throw new RuntimeException(ex);
                        }
                    }

                }).start();
            }
        });
        panel.add(loginButton);

        // 设置窗体内容面板
        frame.add(panel);

        // 设置窗体居中
        frame.setLocationRelativeTo(null);

        // 显示窗体
        frame.setVisible(true);
    }


}